package jdk.lambda.stream;

import jdk.entity.UserEntity;

import java.awt.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * peek案例
 * @author songyafei
 */
public class LambdaPeek {
    public static void main(String[] args) {
        ArrayList<Integer> integerList = new ArrayList<>(Arrays.asList(10,5,6,7));
        integerList.stream()
                .peek(x-> System.out.println("from stream :"+x))
                .map(x-> x + 20)
                .peek(x-> System.out.println("after map : "+x))
                .filter(x->x % 2 ==0)
                .peek(x-> System.out.println("after filter :"+x))
                .limit(2)
                .peek(x-> System.out.println("after limit :"+x))
                .collect(Collectors.toList());

        //场景
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity(1L,18,"zhang", LocalDate.now(),Double.valueOf(1000)));
        userEntities.add(new UserEntity(2L,23,"li", LocalDate.now().minusYears(2),Double.valueOf(1000)));
        userEntities.add(new UserEntity(3L,22,"wang", LocalDate.now().minusYears(3),Double.valueOf(1000)));

        Optional.ofNullable(userEntities).ifPresent(m->{
            List<UserEntity> collect = m.stream().filter(i -> null != i.getUserId())
                    .peek(userEntity -> userEntity.setAge(userEntity.getAge() + 10))
                    .collect(Collectors.toList());
            System.out.println(collect.toString());
        });


    }
}
